package com.riguz.forks.framework.db;

import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.sql.DataSource;

public abstract class PoolManager {
    public static final String DEFAULT_POOL_NAME = "default";

    protected static final ConcurrentMap<String, DataSource> pools = new ConcurrentHashMap<>();

    public PoolManager() {

    }

    public abstract DataSource createPool(String name, Properties config);

    public DataSource createPool(Properties config) {
        return createPool(DEFAULT_POOL_NAME, config);
    }

    public abstract void releasePool(String name);

    public void releasePools(){
        for(String ds:pools.keySet())
            releasePool(ds);
    }

    public DataSource getPool(String name) {
        DataSource pool =  pools.get(name);
        if(pool == null)
            throw new RuntimeException("Pool not exist:" + name);
        return pool;
    }

    public DataSource getPool() {
        return getPool(DEFAULT_POOL_NAME);
    }
}
